package com.shadow.bus.main.work.complete;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.shadow.bus.config.ServiceUrl;
import com.shadow.bus.config.SpKey;
import com.shadow.bus.entity.RepairEntity;
import com.shadow.bus.entity.TroubleEntity;
import com.shadow.bus.main.history.HistoryDelegate;
import com.shadow.bus.main.work.WorkDataMultipleConverter;
import com.tepth.shadow.ui.dialog.sweet.DialogDelegate;
import com.tepth.shadow.ui.dialog.sweet.SweetAlertDialogDelegate;
import com.tepth.shadow.ui.recycler.BaseDecoration;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;
import com.zhangyu.shadow.core.delegate.RxShadowDelegate;
import com.zhangyu.shadow.core.net.callback.IError;
import com.zhangyu.shadow.core.net.callback.ISuccess;
import com.zhangyu.shadow.core.net.rx.RxRestClient;
import com.zhangyu.shadow.core.util.storage.ShadowPreference;

import java.util.ArrayList;
import java.util.List;

import busfix.tepth.com.shadow_bus.R;
import busfix.tepth.com.shadow_bus.R2;
import butterknife.BindView;

/**
 * @author zhangyu
 * @date 2018/7/11
 */

public class WorkCompleteDelegate extends RxShadowDelegate {
    private static final String BUS_CODE = "busCode";
    private static final String BUS_ID = "busId";

    public static WorkCompleteDelegate newInstance(String code, long id) {
        Bundle args = new Bundle();
        args.putString(BUS_CODE, code);
        args.putLong(BUS_ID, id);
        WorkCompleteDelegate fragment = new WorkCompleteDelegate();
        fragment.setArguments(args);
        return fragment;
    }

    @BindView(R2.id.toolbar)
    Toolbar mToolbar;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    WorkCompleteAdapter mCompleteAdapter;
    private String mBusCode;
    private long mBusId;
    private DialogDelegate mDialogDelegate;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mBusCode = bundle.getString(BUS_CODE);
            mBusId = bundle.getLong(BUS_ID);
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_work_complete;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mDialogDelegate.showProgressDialog(true, "正在加载...");
        requestData();
    }

    private void requestData() {
        mDisposables.add(RxRestClient.builder()
                .url(ServiceUrl.REPAIR_INFO)
                .params("authKey", ShadowPreference.getCustomAppProfile(SpKey.TOKEN))
                .params("id", mBusId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        new WorkDataMultipleConverter().convert(response, new WorkDataMultipleConverter.OnConverterListener() {
                            @Override
                            public void onConverterAllData(ArrayList<MultipleItemEntity> list) {
                                mCompleteAdapter.setNewData(list);
                                mDialogDelegate.clearDialog();
                            }

                            @Override
                            public void onConverterTroubles(List<TroubleEntity> troubleEntities) {
                            }

                            @Override
                            public void onConverterRepairInfo(RepairEntity repairEntity) {
                            }

                            @Override
                            public void onConverterError(String msg) {
                                mDialogDelegate.stopProgressWithFailed("请求失败", msg);
                            }
                        });
                        mDialogDelegate.clearDialog();
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        mDialogDelegate.stopProgressWithFailed("请求失败", msg);
                    }
                })
                .build()
                .get());
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mToolbar.inflateMenu(R.menu.history);
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
        mToolbar.setTitle("车辆编号:" + mBusCode + "维修信息");
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int i = item.getItemId();
                if (i == R.id.menu_history) {
                    start(HistoryDelegate.newInstance(mBusCode));
                }
                return true;
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(BaseDecoration.create(getContext().getResources()
                .getColor(R.color.color_white), 2));
        mDialogDelegate = new SweetAlertDialogDelegate(getContext());
        mCompleteAdapter = new WorkCompleteAdapter(null);
        mRecyclerView.setAdapter(mCompleteAdapter);
    }
}
